package org.elec.mac.controller;

import lombok.extern.slf4j.Slf4j;
import org.elec.mac.entity.*;
import org.elec.mac.service.SAttachedService;
import org.elec.mac.service.SFormulaService;
import org.elec.mac.service.SMaterialService;
import org.elec.mac.service.SProductService;
import org.elec.mac.utils.JSONUtil;
import org.elec.mac.utils.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 附料
 * Created by Bean on 2018/3/27.
 */
@Slf4j
@Controller
public class AttachedController extends BaseController {

    @Autowired
    private SAttachedService attachedService;

    @Autowired
    private SProductService productService;

    @Autowired
    private SMaterialService materialService;

    //产品附料列表
    @RequestMapping("/attached_list.do")
    public String formulaListHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                     @RequestParam(value = "action", required = false, defaultValue = "") String action,
                                     @RequestParam(value = "limit", required = false) String limit,
                                     @RequestParam(value = "page", required = false) String page) throws IOException {
        Integer productid = Utils.parseInt(this.getParameter(request, "productid"), 0);
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("id", productid);
        SProduct product = productService.get(param);
        param.clear();
        param.put("pid",  productid);
        List<Map<String, Object>> list = attachedService.getAttachedInfo(param);
        model.put("sProduct", product);
        model.put("attachedList", list);
        return "/attached/attached_list";
    }

    //新增附料
    @RequestMapping(value = "/attached_add.do")
    public void formulaAddHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model,
                                     @RequestParam(value = "action", required = false, defaultValue = "") String action) throws IOException {
        String pid = this.getParameter(request, "productid");
        Map<String, Object> param = new HashMap<>();
        ExecuteResult result = new ExecuteResult();
        result.setSuccess(true);
        result.setMsg("成功");
        if("do".equals(action)) {
            try {
                Long productid =  Utils.parseLong(this.getParameter(request, "productid"),0);
                Long meterialid = Utils.parseLong(this.getParameter(request, "meterialid"),0);
                if(meterialid == 0) {
                    result.setSuccess(false);
                    result.setMsg("附料不能为空");
                }
                if(result.isSuccess()) {
                    SAttached attached = new SAttached();
                    attached.setPid(productid);
                    attached.setMid(meterialid);
                    attachedService.insert(attached);
                    log.info("附料新增");
                }
            } catch(Exception ex) {
                log.error("附料新增错误", ex);
                result.setSuccess(false);
                result.setMsg("附料新增错误");
            }
        }else{
            List<SMaterial> materials = materialService.list(param);
            param.put("pid", Utils.parseLong(this.getParameter(request, "productid"),0));
            List<Map<String, Object>> attacheds = attachedService.getAttachedInfo(param);
            for (int i = 0; i < attacheds.size(); i++) {
                for (int j = 0; j < materials.size(); j++) {
                    if(materials.get(j).getId() == attacheds.get(i).get("mid")) {
                        materials.remove(j);
                        break;
                    }
                }
            }
            result.getBody().put("materList", materials);
        }
        log.info(JSONUtil.object2json(result));
        response.getWriter().write(JSONUtil.object2json(result));
    }

    //删除附料
    @RequestMapping(value = "/attached_delete.do")
    public void attachedDeleteHandler(HttpServletRequest request, HttpServletResponse response, ModelMap model) throws IOException {
        ExecuteResult result = new ExecuteResult();
        result.setSuccess(true);
        result.setMsg("成功");
        try {
            Map<String, Object> params = new HashMap<>();
            params.put("id", Utils.parseLong(request.getParameter("id"), 0L));
            attachedService.delete(params);
            log.info("配方删除");
        } catch(Exception ex) {
            log.error("配方删除错误", ex);
            result.setSuccess(false);
            result.setMsg("配方删除错误");
        }
        log.info(JSONUtil.object2json(result));
        response.getWriter().write(JSONUtil.object2json(result));
    }
}
